<!-- 办理 -->
<template>
  <div v-for="item in formList" :key="item" class="margin-bottom20">
    <iframe v-if="item && (item as string).startsWith('http')" style="width: 100%" :src="item"></iframe>
    <!-- 操作表单 -->
    <!-- <EWHBAP
      v-if="item && (item as string).startsWith('EWHBAP.vue')"
      :processInstanceId="processInstanceId_BAP"
      :taskId="taskId_BAP"
      @submitApprove="submitApprove"
      @showNextTip="showNextTip"
    ></EWHBAP> -->
    <!-- 审批履历 -->
    <approveHistoryByProcessId
      v-if="item && (item as string).startsWith('approveHistoryByProcessId')"
      class="margin-bottom20"
      :processInstanceId="processInstanceId_approve"
      :isRefresh="isRefresh"
    ></approveHistoryByProcessId>
    <approveHistoryByRefBussNo
      v-if="item && (item as string).startsWith('approveHistoryByRefBussNo')"
      class="margin-bottom20"
      :id="refBussNo"
      :isRefresh="isRefresh"
    />
  </div>
</template>
<script setup lang="ts">
import { ElNotification } from 'element-plus'

import { ref, onMounted } from 'vue'
import { apiRequestPost } from '@/api/globalApi.js'
import { useRoute, useRouter } from 'vue-router'
import EWHBAP from '../EWHBAP/index.vue'
import approveHistoryByProcessId from '../../history/approveHistoryByProcessId/index.vue'
import approveHistoryByRefBussNo from '../../history/approveHistoryByRefBussNo/index.vue'
// const processInstanceId = ref('8b9aef14-6033-11ef-afdf-a05950a10a86')
// const refBussNo = ref('100000')
const isRefresh = ref(false)
const route = useRoute()
const router = useRouter()
const processInstanceId = ref(route.query.processInstanceId)
const taskId = ref(route.query.taskId)
const refBussNo = ref(route.query.refBussNo)

const submitApprove = (data: any) => {
  isRefresh.value = true
  console.log(data)
}
// 下一步信息
const visibleNextTip = ref(false)
const loadingNextTip = ref(false)
const loadingText = ref('正在获取下一步执行人，请稍候...')
const getNextTipsNum = ref(1)
const preTip = ref('')
const showTrace = ref(false)
const showNextTip = (e) => {
  //   isRefresh.value = false
  visibleNextTip.value = true
  preTip.value = e
  loadingText.value = `${preTip.value} 正在获取下一步执行人，请稍候...`
  getNextTipInfo()
}
const formList = ref([])
// 搜索查询函数
const getNextTipInfo = () => {
  visibleNextTip.value = true
  showTrace.value = false
  getNextTipsNum.value++
  if (getNextTipsNum.value > 5) {
    nextTipsText.value = `${preTip.value} 获取下一步执行人失败，请查看流程跟踪图：`
    showTrace.value = true
    return
  }
  loadingNextTip.value = true
  apiRequestPost('/bpm-service/BpmApi/getProcessInstanceApproveInfo', {
    processInstanceId: route.query.processInstanceId,
  })
    .then((res) => {
      loadingNextTip.value = false
      if (res.data.success) {
        if (res.data.data == 'waiting') {
          loadingNextTip.value = true
          setTimeout(() => {
            getNextTipInfo()
          }, 2000)
        } else if (res.data.data == 'end') {
          nextTipsText.value = '流程结束'
        } else {
          nextTipsText.value = `${preTip.value} 下一步：${res.data.data}`
        }
      }
    })
    .catch((error: any) => {
      loadingNextTip.value = false
      // 处理错误
    })
}
const goTrace = () => {
  router.push({
    name: 'EWPIGraph',
    query: {
      id: route.query.processInstanceId,
    },
  })
}
const searchFn = () => {
  apiRequestPost('/bpm-service/BpmApi/getTaskIntegrateUrlsForHB', {
    taskId: route.query.taskId,
  }).then((res) => {
    if (res.data.success) {
      formList.value = res.data.data
      initFormData(formList.value)
      console.log(res.data.data)
    }
  })
}
const processInstanceId_BAP = ref(route.query.processInstanceId)
const taskId_BAP = ref(route.query.taskId)
const processInstanceId_approve = ref(route.query.processInstanceId)
const nextTipsText = ref('')
const initFormData = (formList: [] | string[]) => {
  formList.forEach((item: string) => {
    if (item.startsWith('EWHBAP.vue')) {
      const query = new URLSearchParams(item.split('?')[1])
      processInstanceId_BAP.value = query.get('processInstanceId') || route.query.processInstanceId
      taskId_BAP.value = query.get('taskId') || route.query.taskId
    } else if (item.startsWith('approveHistoryByProcessId')) {
      const query = new URLSearchParams(item.split('?')[1])
      processInstanceId_approve.value = query.get('processInstanceId') || route.query.processInstanceId
    } else if (item.startsWith('approveHistoryByRefBussNo')) {
      const query = new URLSearchParams(item.split('?')[1])
      refBussNo.value = query.get('refBussNo') || route.query.refBussNo
    }
  })
}
const refreshIframe = () => {
  var iframes = document.getElementsByTagName('iframe')
  setInterval(function () {
    for (var i = 0, j = iframes.length; i < j; ++i) {
      iframes[i].setAttribute('height', iframes[i].contentWindow.document.body.scrollHeight)
      console.log('iframe-----', iframes)
      iframes[i].setAttribute('height', iframes[i].contentWindow.document.body.scrollHeight)
    }
  }, 2000)
}

onMounted(() => {
  searchFn()
  refreshIframe()
})
</script>
<style lang="less" scoped>
.tarce-class {
  font-size: 16px;
  text-align: center;
  span {
    cursor: pointer;
    color: blue;
    text-decoration: underline;
  }
}
</style>
